package com.hellohoper.test.mybatis.mapper.provider;

import com.hellohoper.test.mybatis.mapper.model.CityDistance;
import org.apache.commons.lang.StringUtils;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class CityDistanceSqlProvider {

    public String selectPageList(CityDistance cityDistance) {

        BEGIN();
        SELECT("id");
        SELECT("dep_city_code");
        SELECT("arr_city_code");
        SELECT("distance");
        SELECT("is_del");
        SELECT("create_time");
        SELECT("update_time");
        SELECT("dep_city_name");
        SELECT("arr_city_name");
        FROM("city_distance");
        WHERE("is_del = 0");
        if (cityDistance != null) {
            if (StringUtils.isNotEmpty(cityDistance.getDepCityCode())) {
                AND();
                WHERE("dep_city_code = #{depCityCode,jdbcType=VARCHAR}");
            }
            if (StringUtils.isNotEmpty(cityDistance.getArrCityCode())) {
                AND();
                WHERE("arr_city_code = #{arrCityCode,jdbcType=VARCHAR}");
            }
        }
        ORDER_BY("id desc");

        return SQL();
    }

    public String insertSelective(CityDistance record) {
        BEGIN();
        INSERT_INTO("city_distance");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getDepCityCode() != null) {
            VALUES("dep_city_code", "#{depCityCode,jdbcType=VARCHAR}");
        }

        if (record.getArrCityCode() != null) {
            VALUES("arr_city_code", "#{arrCityCode,jdbcType=VARCHAR}");
        }

        if (record.getDistance() != null) {
            VALUES("distance", "#{distance,jdbcType=VARCHAR}");
        }

        if (record.getIsDel() != null) {
            VALUES("is_del", "#{isDel,jdbcType=BIT}");
        }

        if (record.getCreateTime() != null) {
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateTime() != null) {
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getDepCityName() != null) {
            VALUES("dep_city_name", "#{depCityName,jdbcType=VARCHAR}");
        }

        if (record.getArrCityName() != null) {
            VALUES("arr_city_name", "#{arrCityName,jdbcType=VARCHAR}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(CityDistance record) {
        BEGIN();
        UPDATE("city_distance");

        if (record.getDepCityCode() != null) {
            SET("dep_city_code = #{depCityCode,jdbcType=VARCHAR}");
        }

        if (record.getArrCityCode() != null) {
            SET("arr_city_code = #{arrCityCode,jdbcType=VARCHAR}");
        }

        if (record.getDistance() != null) {
            SET("distance = #{distance,jdbcType=VARCHAR}");
        }

        if (record.getIsDel() != null) {
            SET("is_del = #{isDel,jdbcType=BIT}");
        }

        if (record.getCreateTime() != null) {
            SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateTime() != null) {
            SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getDepCityName() != null) {
            SET("dep_city_name = #{depCityName,jdbcType=VARCHAR}");
        }

        if (record.getArrCityName() != null) {
            SET("arr_city_name = #{arrCityName,jdbcType=VARCHAR}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }

}